from TextBadger.models import *
from xml.dom.minidom import parse
import datetime
from ..settings import *
import sys
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.utils.encoding import smart_str

corpora = Corpora.objects.filter(status=2)

for corpus in corpora:
	corpus.status=3
	corpus.save()

for corpus in corpora:
	print corpus.id
	xmlfile = corpus.corpus_file.path
	dom = parse(xmlfile)
	try:
		desc = dom.getElementsByTagName("description")[0].childNodes[0].data
	except:
		desc = ''
	
	if desc != '':
		corpus.description = desc		
	
	try:
		ndocs = int(dom.getElementsByTagName("documents")[0].attributes['num_docs'].value)
	except:
		ndocs = -1
	try:
		docs = dom.getElementsByTagName("documents")[0].getElementsByTagName("document")
		num_docs = len(docs)
		corpus.num_docs = num_docs

		for doc in docs:
			doc_text = doc.getElementsByTagName("text")[0].childNodes[0].data
			# save doc_text in a file and make a Document object
			now = datetime.datetime.now().microsecond
			d = corpus.Documents.create(filename=default_storage.save(MEDIA_ROOT + "TextBadger/tmp/" + str(now) + ".html" , ContentFile(smart_str(doc_text))))
		corpus.status = 1
		corpus.save()	
	except:
		print sys.exc_type, ":", sys.exc_value
		print 'Parse Error. Marking this corpus as invalid'
		corpus.status = 4
		corpus.save()

	
